import Vue from 'vue'
import Vuex from 'vuex'
import router from '@/router'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    Page: [],
    path: '' // 当前页面所在的路径
  },
  getters: {
  },
  mutations: {
    setpath (state, path) { // 设置当前路由窗口
      state.path = path
    },
    AddPage (state, item) {
      state.Page.push(item)
    },
    DelPage (state, item) {
      // console.log(state.Page)
      // console.log(item)
      // console.log(state.Page.indexOf(item))
      state.Page.splice(state.Page.indexOf(item), 1)
      this.commit('beforeDel')
      // state.path = this.state.Page.at(-1)?.path || '/layout/dashboard'
      // // console.log(router)
      // console.log(this)
      // router.push(state.path)
    },
    CloseAll (state, item) {
      state.Page = []
      this.commit('beforeDel')
    },
    beforeDel (state) { // 关闭某个窗口的时候 调用 这个方法.将state.path设置成page不为空时的最后一项或dashboard
      state.path = this.state.Page.at(-1)?.path || '/layout/dashboard'
      // console.log(router)
      console.log(this)
      router.push(state.path)
    },
    CloseLeft (state, path) { // 关闭左侧的窗口
      // console.log(state.Page.filter(i => i.path === path))
      // console.log(state.Page.filter(i => i.path === path));
      state.Page.splice(0, state.Page.indexOf(state.Page.filter(i => i.path === path)[0]))
      this.commit('beforeDel')
    },
    CloseRight (state, path) { // 关闭右侧窗口
      console.log(1)
      console.log(state.Page.filter(i => i.path === path))
      console.log(state.Page.indexOf(state.Page.filter(i => i.path === path)[0]))
      // console.log(state.Page.filter(i => i.path === path));
      state.Page.splice(state.Page.indexOf(state.Page.filter(i => i.path === path)[0]), state.Page.length - 1)
      this.commit('beforeDel')
    },
    CloseOthers (state, path) {
      state.Page = state.Page.filter(i => i.path === path)
      this.commit('beforeDel')
    }
  },
  actions: {
    asyncAddPage (context, payload) {
      // console.log(payload)
      context.commit('AddPage', payload)
    },
    asyncDelPage (context, payload) {
      context.commit('DelPage', payload)
    },
    asyncCloseAll (context, payload) {
      context.commit('CloseAll', payload)
    },
    asyncsetpath (context, payload) { // 设置当前路由窗口
      context.commit('setpath', payload)
    },
    asyncCloseLeft (context, payload) {
      context.commit('CloseLeft', payload)
    },
    asyncCloseRight (context, payload) {
      console.log(1)
      context.commit('CloseRight', payload)
    },
    asyncCloseOthers (context, payload) {
      context.commit('CloseOthers', payload)
    }
  },
  modules: {
  }
})
